﻿using System.Collections;
using System.Text.RegularExpressions;

namespace Als.Common.Image
{
    public static class BarCodeHelper
    {

        /// <summary>
        /// 条形码 39码(HTML代码)
        /// </summary>
        /// <param name="s"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static string Get39(this string s, int width, int height)
        {
            var ht = new Hashtable
            {
                {'A', "110101001011"},
                {'B', "101101001011"},
                {'C', "110110100101"},
                {'D', "101011001011"},
                {'E', "110101100101"},
                {'F', "101101100101"},
                {'G', "101010011011"},
                {'H', "110101001101"},
                {'I', "101101001101"},
                {'J', "101011001101"},
                {'K', "110101010011"},
                {'L', "101101010011"},
                {'M', "110110101001"},
                {'N', "101011010011"},
                {'O', "110101101001"},
                {'P', "101101101001"},
                {'Q', "101010110011"},
                {'R', "110101011001"},
                {'S', "101101011001"},
                {'T', "101011011001"},
                {'U', "110010101011"},
                {'V', "100110101011"},
                {'W', "110011010101"},
                {'X', "100101101011"},
                {'Y', "110010110101"},
                {'Z', "100110110101"},
                {'0', "101001101101"},
                {'1', "110100101011"},
                {'2', "101100101011"},
                {'3', "110110010101"},
                {'4', "101001101011"},
                {'5', "110100110101"},
                {'6', "101100110101"},
                {'7', "101001011011"},
                {'8', "110100101101"},
                {'9', "101100101101"},
                {'+', "100101001001"},
                {'-', "100101011011"},
                {'*', "100101101101"},
                {'/', "100100101001"},
                {'%', "101001001001"},
                {'$', "100100100101"},
                {'.', "110010101101"},
                {' ', "100110101101"}
            };
            s = "*" + s.ToUpper() + "*";
            var resultBin = "";//二进制串
            try
            {
                foreach (var ch in s)
                {
                    resultBin += ht[ch].ToString();
                    resultBin += "0";//间隔，与一个单位的线条宽度相等
                }
            }
            catch { return "存在不允许的字符！"; }

            var resultHtml = ""; //HTML代码
            foreach (var c in resultBin)
            {
                var color = c == '0' ? "#FFFFFF" : "#000000";
                resultHtml += "<div style=\"width:" + width + "px;height:" + height + "px;float:left;background:" + color + ";\"></div>";
            }
            resultHtml += "<div style=\"clear:both\"></div>";

            var len = ht['*'].ToString().Length;
            foreach (var c in s)
            {
                resultHtml += "<div style=\"width:" + (width * (len + 1)) + "px;float:left;color:#000000;text-align:center;\">" + c + "</div>";
            }
            resultHtml += "<div style=\"clear:both\"></div>";

            return "<div style=\"background:#FFFFFF;padding:5px;font-size:" + (width * 10) + "px;font-family:'楷体';\">" + resultHtml + "</div>";
        }

        /// <summary>
        /// 条形码 EAN码(HTML代码)
        /// </summary>
        /// <param name="s"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static string GetEan13(this string s, int width, int height)
        {
            var checkcodeInput = -1;//输入的校验码
            if (!Regex.IsMatch(s, @"^\d{12}$"))
            {
                if (!Regex.IsMatch(s, @"^\d{13}$"))
                {
                    return "存在不允许的字符！";
                }
                else
                {
                    checkcodeInput = int.Parse(s[12].ToString());
                    s = s.Substring(0, 12);
                }
            }
            var sumEven = 0;//偶数位之和
            var sumOdd = 0; //奇数位之和
            for (var i = 0; i < 12; i++)
            {
                if (i % 2 != 0)
                    sumEven += int.Parse(s[i].ToString());
                else
                    sumOdd += int.Parse(s[i].ToString());
            }
            var checkcode = (10 - (sumEven * 3 + sumOdd) % 10) % 10;//校验码
            if (checkcodeInput > 0 && checkcodeInput != checkcode)
            {
                return "输入的校验码错误！";
            }
            s += checkcode;//变成13位
            // 000000000101左侧42个01010右侧35个校验7个101000000000
            // 6        101左侧6位 01010右侧5位校验1位101000000000
            var resultBin = "";//二进制串
            resultBin += "000000000101";
            var type = Ean13Type(s[0]);
            for (var i = 1; i < 7; i++)
            {
                resultBin += Ean13(s[i], type[i - 1]);
            }
            resultBin += "01010";
            for (var i = 7; i < 13; i++)
            {
                resultBin += Ean13(s[i], 'C');
            }
            resultBin += "101000000000";
            string resultHtml = ""; //HTML代码
            int heightBottom = width * 5;
            foreach (var c in resultBin)
            {
                var color = c == '0' ? "#FFFFFF" : "#000000";       //颜色
                resultHtml += "<div style=\"width:" + width + "px;height:" + height + "px;float:left;background:" + color + ";\"></div>";
            }
            resultHtml += "<div style=\"clear:both\"></div>";
            resultHtml += "<div style=\"float:left;color:#000000;width:" + (width * 9) + "px;text-align:center;\">" + s[0] + "</div>";
            resultHtml += "<div style=\"float:left;width:" + width + "px;height:" + heightBottom + "px;background:#000000;\"></div>";
            resultHtml += "<div style=\"float:left;width:" + width + "px;height:" + heightBottom + "px;background:#FFFFFF;\"></div>";
            resultHtml += "<div style=\"float:left;width:" + width + "px;height:" + heightBottom + "px;background:#000000;\"></div>";
            for (var i = 1; i < 7; i++)
            {
                resultHtml += "<div style=\"float:left;width:" + (width * 7) + "px;color:#000000;text-align:center;\">" + s[i] + "</div>";
            }
            resultHtml += "<div style=\"float:left;width:" + width + "px;height:" + heightBottom + "px;background:#FFFFFF;\"></div>";
            resultHtml += "<div style=\"float:left;width:" + width + "px;height:" + heightBottom + "px;background:#000000;\"></div>";
            resultHtml += "<div style=\"float:left;width:" + width + "px;height:" + heightBottom + "px;background:#FFFFFF;\"></div>";
            resultHtml += "<div style=\"float:left;width:" + width + "px;height:" + heightBottom + "px;background:#000000;\"></div>";
            resultHtml += "<div style=\"float:left;width:" + width + "px;height:" + heightBottom + "px;background:#FFFFFF;\"></div>";
            for (var i = 7; i < 13; i++)
            {
                resultHtml += "<div style=\"float:left;width:" + (width * 7) + "px;color:#000000;text-align:center;\">" + s[i] + "</div>";
            }
            resultHtml += "<div style=\"float:left;width:" + width + "px;height:" + heightBottom + "px;background:#000000;\"></div>";
            resultHtml += "<div style=\"float:left;width:" + width + "px;height:" + heightBottom + "px;background:#FFFFFF;\"></div>";
            resultHtml += "<div style=\"float:left;width:" + width + "px;height:" + heightBottom + "px;background:#000000;\"></div>";
            resultHtml += "<div style=\"float:left;color:#000000;width:" + (width * 9) + "px;\"></div>";
            resultHtml += "<div style=\"clear:both\"></div>";

            return "<div style=\"background:#FFFFFF;padding:0px;font-size:" + (width * 10) + "px;font-family:'楷体';\">" + resultHtml + "</div>";
        }

        private static string Ean13(char c, char type)
        {
            switch (type)
            {
                case 'A':
                    {
                        switch (c)
                        {
                            case '0': return "0001101";
                            case '1': return "0011001";
                            case '2': return "0010011";
                            case '3': return "0111101";
                            case '4': return "0100011";
                            case '5': return "0110001";
                            case '6': return "0101111";
                            case '7': return "0111011";
                            case '8': return "0110111";
                            case '9': return "0001011";
                            default: return "Error!";
                        }
                    }
                case 'B':
                    {
                        switch (c)
                        {
                            case '0': return "0100111";
                            case '1': return "0110011";
                            case '2': return "0011011";
                            case '3': return "0100001";
                            case '4': return "0011101";
                            case '5': return "0111001";
                            case '6': return "0000101";
                            case '7': return "0010001";
                            case '8': return "0001001";
                            case '9': return "0010111";
                            default: return "Error!";
                        }
                    }
                case 'C':
                    {
                        switch (c)
                        {
                            case '0': return "1110010";
                            case '1': return "1100110";
                            case '2': return "1101100";
                            case '3': return "1000010";
                            case '4': return "1011100";
                            case '5': return "1001110";
                            case '6': return "1010000";
                            case '7': return "1000100";
                            case '8': return "1001000";
                            case '9': return "1110100";
                            default: return "Error!";
                        }
                    }
                default: return "Error!";
            }
        }

        private static string Ean13Type(char c)
        {
            switch (c)
            {
                case '0': return "AAAAAA";
                case '1': return "AABABB";
                case '2': return "AABBAB";
                case '3': return "AABBBA";
                case '4': return "ABAABB";
                case '5': return "ABBAAB";
                case '6': return "ABBBAA";
                case '7': return "ABABAB";
                case '8': return "ABABBA";
                case '9': return "ABBABA";
                default: return "Error!";
            }
        }



    }


}
